package servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Appointment;
import entity.FootballMatch;
import entity.Players;
import entity.User;

import session.AppointmentDao;
import session.FootballMatchDao;
import session.PlayersDao;
import session.UserDao;

 

/**
 * Kontroler za administraciju fudbalskih mečeva
 * 
 * @author rigo & jung
 */
public class AdminMatch extends HttpServlet {
	private static final long serialVersionUID = -9026278194446517234L;
	
	@EJB
	protected UserDao userDao;
	
	@EJB
	protected PlayersDao playersDao ;
	 
	@EJB
	protected AppointmentDao appointmentDao;
	
	@EJB
	protected FootballMatchDao footballMatchDao;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			User admin;
			if((admin = (User)request.getSession().getAttribute("user"))==null ||
					admin.getType()!='A'){
				response.sendRedirect(response.encodeURL("./login.jsp"));
				return;
			}
			
			Appointment app = appointmentDao.findById(Integer.parseInt(request.getParameter("appId")));
			request.setAttribute("appointment", app);
			
			/*
			 * preusmeravanje na formu za dodavanje učesnika 
			 */			
			if(request.getParameter("addPlayer")!=null && request.getParameter("addPlayer").equals("true")){
				request.setAttribute("users", userDao.getAllActive());
				getServletContext().getRequestDispatcher("/adminAddPlayer.jsp").forward(request, response);
				return;
			}
			/*
			 * Dodavanje učesnika koji nisu bili prijaviljeni 
			 */
			if(request.getParameter("adding")!=null && request.getParameter("adding").equals("true")){
				Integer id = Integer.parseInt(request.getParameter("idUsr"));
				User usr = userDao.findById(id);
				Players plAdd = new Players();
				plAdd.setUser(usr);
				plAdd.setAppointment(app);
				plAdd.setParticipated(true);
				plAdd.setGoal(0);
				plAdd.setVote(false);
				plAdd.setMvp(0);
				playersDao.merge(plAdd);
			}
			/*
			 * Dodavanje korisnika u domaći tim
			 * 
			 */
			if(request.getParameter("homeId")!=null && !request.getParameter("homeId").equals("")){
				Integer plId = Integer.parseInt(request.getParameter("homeId"));
				Players player = playersDao.findById(plId);
				player.setTeam('H');
				playersDao.merge(player);
			}
			
			/*
			 * Dodavanje korisika u gostujući tim
			 * 
			 */
			if(request.getParameter("guestId")!=null && !request.getParameter("guestId").equals("")){
				Integer plId = Integer.parseInt(request.getParameter("guestId"));
				Players player = playersDao.findById(plId);
				player.setTeam('G');
				playersDao.merge(player);
			}
			
			/*
			 * Postigunuti golovi korisnika na utakmici
			 * 
			 */
			if(request.getParameter("goalId")!=null && !request.getParameter("goalId").equals("")){
				Integer plId = Integer.parseInt(request.getParameter("goalId"));
				Players player = playersDao.findById(plId);
				User usser = userDao.findById(player.getUser().getUserId());
				usser.setGoals(usser.getGoals()+1);
				userDao.merge(usser);
				
				player.setGoal(player.getGoal()+1);
				playersDao.merge(player);
			}
			
			/*
			 * Smanjenje postigunutih golova korisnika na utakmici 
			 * 
			 */
			if(request.getParameter("notGoalId")!=null && !request.getParameter("notGoalId").equals("")){
				Integer plId = Integer.parseInt(request.getParameter("notGoalId"));
				Players player = playersDao.findById(plId);
				User usser = userDao.findById(player.getUser().getUserId());
				if(player.getGoal()>0){
					usser.setGoals(usser.getGoals()-1);
					userDao.merge(usser);
				
					player.setGoal(player.getGoal()-1);
					playersDao.merge(player);
				}
			}
						
			/*
			 * Označavanje korisnika koji se prijavio ali nije prisustovao na meču 
			 */
			
			if(request.getParameter("playersId")!=null && !request.getParameter("playersId").equals("")){
				Integer plId = Integer.parseInt(request.getParameter("playersId"));
				Players player = playersDao.findById(plId);
				player.setParticipated(false);
				playersDao.merge(player);
			}
			/*
			 * Zaključavanje meča, aktiviranje glasanje
			 * završetak glasanja za prethodne mečev
			 * izbor prethodnog Mvpa
			 * 
			 */
			if(request.getParameter("generate")!=null && request.getParameter("generate").equals("true")){
				
				app.setExpired(true);
				appointmentDao.merge(app);
				
				String name = request.getParameter("name");
				FootballMatch fMatch = new FootballMatch();
				fMatch.setAppointment(app);
				fMatch.setName(name);
				fMatch.setGoalHome(playersDao.sumGoals(app, 'H'));
				fMatch.setGoalGuest(playersDao.sumGoals(app, 'G'));
				footballMatchDao.deactivateAll();
				fMatch.setVotingActive(true);
				footballMatchDao.persist(fMatch);
				response.sendRedirect(response.encodeURL("./ListPastAppointments"));
				return;
			}
			
			request.setAttribute("players", playersDao.findAllByAppointment(app));
			
			getServletContext().getRequestDispatcher("/adminMatchDetails.jsp").forward(request, response);
			
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
